﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace CRM.BLL
{
    public class ServiceUser
    {
        /// <summary>
        /// 获取所有的业务人员信息
        /// </summary>
        public static SqlDataReader GetAllServiceUserInfo(string strServiceID)
        {
            try
            {
                //根据存储过程要传入的参数信息构建SqlParameter数组实例
                SqlParameter[] strPar = {
					new SqlParameter("@T_ServiceID", SqlDbType.VarChar,10)};
                strPar[0].Value = strServiceID;  //为参数数组赋值
                //执行GetServiceUserInfo存储过程返回结果
                return DBUtility.SqlHelper.ExecuteReader
                    (DBUtility.SqlHelper.sqlConnectionString, 
                    CommandType.StoredProcedure, "GetServiceUserInfo", strPar);
            }
            catch (Exception ex)
            {
                throw ex;      //抛出异常消息
            }
        }
        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="strUserID"></param>
        /// <returns></returns>
        public static bool DelUserIDByID(string strUserID)
        {
            try
            {   //根据传入的以逗号分隔的客户列表构建SqlParameter数组
                SqlParameter[] strPar = {
					new SqlParameter("@T_UserID", SqlDbType.Char,1000)};
                strPar[0].Value = strUserID;
                //执行存储过程进行删除工作
                return (DBUtility.SqlHelper.ExecuteNonQuery
                    (DBUtility.SqlHelper.sqlConnectionString, 
                    CommandType.StoredProcedure, 
                    "DelUserInfo", strPar) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;          //捕捉异常并重新抛出
            }
        }
        /// <summary>
        /// 更新客户信息状态
        /// </summary>
        /// <param name="strUserID"></param>
        /// <returns></returns>
        public static bool UpdateUserIDByID(string strUserID, string strUpdateMes)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_UserID", SqlDbType.Char,1000),
					new SqlParameter("@T_UpdateMes", SqlDbType.Char,100)};
                strPar[0].Value = strUserID;
                strPar[1].Value = strUpdateMes;
                return (DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "UpdateUserStateInfo", strPar) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取一条客户信息
        /// </summary>
        /// <returns></returns>
        public static SqlDataReader GetServiceUserInfo(string T_UserID)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_UserID", SqlDbType.Char,10)};
                strPar[0].Value = T_UserID;
                return DBUtility.SqlHelper.ExecuteReader(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "GetServiceUserInfoByID", strPar);
            }
            catch (Exception EX)
            {
                throw EX;
            }
        }

        /// <summary>
        /// 获取一条工作记录信息
        /// </summary>
        /// <returns></returns>
        public static SqlDataReader GetServiceWorkInfo(string T_UserID,string T_ServiceID)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_UserID", SqlDbType.Char,10),
					new SqlParameter("@T_ServiceID", SqlDbType.Char,10)};
                strPar[0].Value = T_UserID;
                strPar[1].Value = T_ServiceID;
                return DBUtility.SqlHelper.ExecuteReader(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "GetServiceWorkInfoByID", strPar);
            }
            catch (Exception EX)
            {
                throw EX;
            }
        }
        /// <summary>
        /// 编辑用户角色信息
        /// </summary>
        /// <param name="PowerModel"></param>
        /// <returns></returns>
        public static bool UpdatePowerMes(Model.T_ServicePower PowerModel)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_PowerID", SqlDbType.Char,10),
					new SqlParameter("@T_PowerName", SqlDbType.NChar,10),
					new SqlParameter("@T_PowerMes", SqlDbType.Char,100),
					new SqlParameter("@T_Remark", SqlDbType.NChar,50)};
                strPar[0].Value = PowerModel.T_PowerID;
                strPar[1].Value = PowerModel.T_PowerName;
                strPar[2].Value = PowerModel.T_PowerMes;
                strPar[3].Value = PowerModel.T_Remark;
                return (DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "EditUserPowerMes", strPar) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="strPID"></param>
        /// <returns></returns>
        public static SqlDataReader GetPowerInfo(string strPID)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_PowerID", SqlDbType.Char,10)};
                strPar[0].Value = strPID;
                return DBUtility.SqlHelper.ExecuteReader(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "GetPowerUserInfoByID", strPar);
            }
            catch (Exception EX)
            {
                throw EX;
            }
        }


        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="strPID"></param>
        /// <returns></returns>
        public static bool DelPowerInfo(string strPID)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_PowerID", SqlDbType.Char,10)};
                strPar[0].Value = strPID;
                return (DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "DelPowerUserInfoByID", strPar) > 0) ? true : false;
            }
            catch (Exception EX)
            {
                throw EX;
            }
        }
        /// <summary>
        /// 编辑人员信息
        /// </summary>
        /// <param name="ServiceModel"></param>
        /// <returns></returns>
        public static bool EditServiceUserInfo(Model.T_ServiceUser ServiceModel)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_ServiceName", SqlDbType.NChar,10),
					new SqlParameter("@T_PowerID", SqlDbType.Int,4),
					new SqlParameter("@T_ServicePass", SqlDbType.Char,100),
					new SqlParameter("@T_ServiceMail", SqlDbType.Char,10),
					new SqlParameter("@T_ServiceQQ", SqlDbType.Char,10),
					new SqlParameter("@T_ServiceTel", SqlDbType.Char,10),
					new SqlParameter("@T_ServiceAdress", SqlDbType.NChar,100),
					new SqlParameter("@T_ServicebirthDay", SqlDbType.NChar,10),
					new SqlParameter("@T_ServiceState", SqlDbType.TinyInt,1),
					new SqlParameter("@T_Remark", SqlDbType.NChar,50),
					new SqlParameter("@T_ServiceID", SqlDbType.NChar,10)};
                strPar[0].Value = ServiceModel.T_ServiceName;
                strPar[1].Value = ServiceModel.T_PowerID;
                strPar[2].Value = ServiceModel.T_ServicePass;
                strPar[3].Value = ServiceModel.T_ServiceMail;
                strPar[4].Value = ServiceModel.T_ServiceQQ;
                strPar[5].Value = ServiceModel.T_ServiceTel;
                strPar[6].Value = ServiceModel.T_ServiceAdress;
                strPar[7].Value = ServiceModel.T_ServicebirthDay;
                strPar[8].Value = ServiceModel.T_ServiceState;
                strPar[9].Value = ServiceModel.T_Remark;
                strPar[10].Value = ServiceModel.T_ServiceID;
                return (DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "EditServiceUserInfo", strPar) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除人员信息
        /// </summary>
        /// <param name="strUserID"></param>
        /// <returns></returns>
        public static bool DelServiceUserByID(string stServiceID)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_ServiceID", SqlDbType.Char,10)};
                strPar[0].Value = stServiceID;
                return (DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "DelServiceUserInfo", strPar) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 检测该人员注册名是否存在
        /// </summary>
        /// <param name="strServiceName"></param>
        /// <returns></returns>
        public static string CheckServiceName(string strServiceName)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_ServiceName", SqlDbType.VarChar,10)};
                strPar[0].Value = strServiceName;
                return DBUtility.SqlHelper.ExecuteScalar(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "CheckServiceName", strPar).ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 写入工作记录信息
        /// </summary>
        /// <param name="strUserID"></param>
        /// <param name="strServiceID"></param>
        public static void InsertWorkLog(string strUserID, string strServiceID)
        {
            string[] strWorkLog = null;
            if (strUserID.IndexOf(",") > 0)
            {
                strWorkLog = strUserID.Split(',');
            }
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_UserID", SqlDbType.VarChar,10),
					new SqlParameter("@T_ServiceID", SqlDbType.VarChar,10)};
                strPar[1].Value = strServiceID;
                if (strUserID.IndexOf(",") > 0)
                {
                    for (int i = 0; i < strWorkLog.Length; i++)
                    {
                        strPar[0].Value = strWorkLog[i];
                        DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "InsertWorkLog", strPar);
                    }
                }
                else
                {
                        strPar[0].Value = strUserID;
                        DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "InsertWorkLog", strPar);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 更新工作记录
        /// </summary>
        /// <param name="strLogID"></param>
        /// <param name="strState"></param>
        /// <param name="strNote"></param>
        public static void UpdateServiceWorkLog(string strLogID,string strState,string strNote)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_SLogID", SqlDbType.VarChar,10),
					new SqlParameter("@T_SLogState", SqlDbType.VarChar,10),
					new SqlParameter("@T_BusinessLogNote", SqlDbType.NVarChar,2000)};
                strPar[0].Value = strLogID;
                strPar[1].Value = strState;
                strPar[2].Value = strNote;
                DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "UpdateServiceWorkLog", strPar);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 更新客户信息状态
        /// </summary>
        /// <param name="strLogID"></param>
        /// <param name="strState"></param>
        /// <param name="strNote"></param>
        public static void UpdateUserState( string strUserID)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_UserID", SqlDbType.VarChar,10)};
                strPar[0].Value = strUserID;
                DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "UpdateUserFree", strPar);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 用户登录验证 返回用户ID，职位，权限信息 数组
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="strPass"></param>
        /// <returns></returns>
        public static SqlDataReader UserLogin(string strUserName, string strPass)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_ServiceName", SqlDbType.VarChar,10),
					new SqlParameter("@T_ServicePass", SqlDbType.VarChar,100)};
                strPar[0].Value = strUserName;
                strPar[1].Value = Common.EncryptString.EncryptMD5(strPass);
                return DBUtility.SqlHelper.ExecuteReader(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "CheckUserLogin", strPar);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 用户自行修改密码
        /// </summary>
        /// <param name="strServiceID"></param>
        /// <param name="strServicePass"></param>
        /// <param name="strNewPass"></param>
        /// <returns></returns>
        public static bool UpdateUserPass(string strServiceID, string strServicePass, string strNewPass)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_ServiceID", SqlDbType.VarChar,10),
					new SqlParameter("@T_ServicePass", SqlDbType.VarChar,100),
					new SqlParameter("@T_NewPass", SqlDbType.VarChar,100)};
                strPar[0].Value = strServiceID;
                strPar[1].Value = Common.EncryptString.EncryptMD5(strServicePass);
                strPar[2].Value = Common.EncryptString.EncryptMD5(strNewPass);
                return (DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "UpdateUserPass", strPar) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 管理员重置用户密码
        /// </summary>
        /// <param name="strServiceID"></param>
        /// <param name="strServicePass"></param>
        /// <param name="strNewPass"></param>
        /// <returns></returns>
        public static bool UpdateUserPass(string strServiceID,string strNewPass)
        {
            try
            {
                SqlParameter[] strPar = {
					new SqlParameter("@T_ServiceID", SqlDbType.VarChar,10),
					new SqlParameter("@T_NewPass", SqlDbType.VarChar,100)};
                strPar[0].Value = strServiceID;
                strPar[1].Value = Common.EncryptString.EncryptMD5(strNewPass);
                return (DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.sqlConnectionString, CommandType.StoredProcedure, "ResetUserPass", strPar) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
